<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 重写 Model 类
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: model.php 128 2012-04-01 04:39:21Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Model
{

    /**
     * @var instances of model
     */
    protected static $_instances = array();

    /**
     * Create a new model instance.
     *
     *     $model = Model::factory($name);
     *
     * @param   string   model name
     * @return  Model
     */
    public static function factory($name)
    {
        // Add the model prefix
        $class = 'Model_'.$name;

        if ( ! isset(self::$_instances[$class]))
        {
            self::$_instances[$class] = new $class;
        }

        return self::$_instances[$class];
    }

    /**
     * @var Auth
     */
    public $auth = NULL;

    /**
     * @var Session_NameSpace
     */
    public $session = NULL;

    /**
     * Error list, field
     *
     * @var array
     */
    protected $_errors = array();

    /**
     * @var Validation
     */
    protected $_validation = NULL;

    /**
     * 构造方法
     */
    public function __construct()
    {
        $this->auth = Auth::instance();
        $this->session = Session_NameSpace::factory('model');
    }

    /**
     * Add an error to a field.
     *
     * @param   string  field name
     * @param   string  error message
     * @param   string  params for error
     * @return  $this
     */
    public function error($field, $error, array $params = NULL)
    {
        $this->_errors[$field] = array($error, $params);

        return FALSE;
    }

    /**
     * Returns the error messages.
     *
     * @param   string  file to load error messages from
     * @param   mixed   translate the message
     * @return  array
     */
    public function errors($file = NULL, $translate = TRUE)
    {
        $validation = $this->validation();
        if ( ! $validation)
        {
           $validation = Validation::factory(array());
        }

        foreach ($this->_errors as $field => $error)
        {
            list($error, $params) = $error;
            $validation->label($field, $field)->error($field, $error, $params);
        }

        return $validation->errors($file, $translate);
    }

    /**
     * Returns the first error message.
     *
     * @param   string  file to load error messages from
     * @param   mixed   translate the message
     * @return  array
     */
    public function first_error($file = NULL, $translate = TRUE)
    {
        return current($this->errors($file, $translate));
    }

    /**
     * Returns the last error message.
     *
     * @param   string  file to load error messages from
     * @param   mixed   translate the message
     * @return  array
     */
    public function last_error($file = NULL, $translate = TRUE)
    {
        $errors = $this->errors($file, $translate);

        return end($errors);
    }

    /**
     * Set / Get the [Validation] instance
     *
     * @param  Validation $validation
     * @return Validation
     */
    public function validation(Validation $validation = NULL)
    {
        if ($validation instanceof Validation)
        {
            $this->_validation = $validation;
        }

        return $this->_validation;
    }
}